//
//  MBLoginDataManager.m
//  Meboo
//
//  Created by kyo on 2023/10/16.
//

#import "MBChatDataManager.h"
#import "MBHTTPNetworking.h"
@implementation MBChatDataManager
+ (MBChatDataManager *)sharedManager {
    static dispatch_once_t onceToken;
    static MBChatDataManager *obj;
    dispatch_once(&onceToken, ^{
        obj = [[self alloc] init];
    });
    return obj;
}

#pragma mark -获取融云token
- (void)getRongCloudToken:(void(^)(BOOL success))resultBlock {
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [[MBHTTPNetworking sharedInstance] WSAFNoOAuthUrlString:@"api/auth/getIMtoken" method:@"POST" parameters:params success:^(id  _Nullable responseObject) {
        NSInteger code = [responseObject[@"code"] integerValue];
        if (code == 0) {
            NSString *tokenStr = [NSString stringWithFormat:@"%@",responseObject[@"data"][@"token"]];
            MBUserAccount  *account = [MBUserAccount defaultAccount];
            account.rongCloudToken = tokenStr;
            [MBUserAccount  saveAccount:account];
            if (resultBlock) resultBlock(YES);
        } else {
            if (resultBlock) resultBlock(NO);
        }
    } failure:^(NSError * _Nullable error) {
        if (resultBlock) resultBlock(NO);
    }];
}

#pragma mark -获取新朋友列表
- (void)getNewFriendsList:(void(^)(BOOL success))resultBlock {
    __WeakSelf
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [params setObject:@1 forKey:@"pageNum"];
    [params setObject:@20 forKey:@"pageSize"];
    [[MBHTTPNetworking sharedInstance] WSAFNoOAuthUrlString:@"api/users/matchedList" method:@"POST" parameters:params success:^(id  _Nullable responseObject) {
        NSInteger code = [responseObject[@"code"] integerValue];
        if (code == 0) {
            [weakSelf.newFriendsDataArr removeAllObjects];
            
            NSString *newFlag = @"";
            NSString *online = @"";
            NSString *nearby = @"";
            for (NSDictionary *modelDict in responseObject[@"data"]) {
                newFlag = [NSString stringWithFormat:@"%@",[modelDict valueForKey:@"newFlag"]];
                online = [NSString stringWithFormat:@"%@",[modelDict valueForKey:@"online"]];
                nearby = [NSString stringWithFormat:@"%@",[modelDict valueForKey:@"nearby"]];
                MBChatNewFriendsModel *model = [MBChatNewFriendsModel mj_objectWithKeyValues:modelDict];
                
                
                if ([newFlag isEqualToString:@"1"]) {
                    model.newflag = YES;
                } else {
                    model.newflag = NO;
                }
                
                if ([online isEqualToString:@"1"]) {
                    model.online = YES;
                } else {
                    model.online = NO;
                }
                
                if ([nearby isEqualToString:@"1"]) {
                    model.nearby = YES;
                } else {
                    model.nearby = NO;
                }
                [weakSelf.newFriendsDataArr addObject:model];
            }
            
            if (weakSelf.newFriendsDataArr.count > 0 && weakSelf.onlineCodeArr.count > 0) {
                for (MBChatNewFriendsModel *model in weakSelf.newFriendsDataArr) {
                    if ([self.onlineCodeArr containsObject:model.userCode]) {
                        model.online = YES;
                    } else {
                        model.online = NO;
                    }
                }
            }
            
            if (resultBlock) resultBlock(YES);
        } else {
            if (resultBlock) resultBlock(NO);
        }
    } failure:^(NSError * _Nullable error) {
        if (resultBlock) resultBlock(NO);
    }];
}

#pragma mark -获取用户信息 根据用户id
- (void)getUserInfoWithUserId:(NSString *)userId completion:(void (^)(RCUserInfo *userInfo))completion {
    if (userId.length == 0) {
        userId = @"";
    }
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [params setObject:userId forKey:@"userCode"];
    [[MBHTTPNetworking sharedInstance] WSAFNoOAuthUrlString:@"api/users/getUserInfo" method:@"POST" parameters:params success:^(id  _Nullable responseObject) {
        NSInteger code = [responseObject[@"code"] integerValue];
        RCUserInfo *userInfo = [RCUserInfo new];
        if (code == 0) {
            userInfo.name = [NSString stringWithFormat:@"%@",responseObject[@"data"][@"nickName"]];
            userInfo.userId = userId;
            userInfo.portraitUri = [NSString stringWithFormat:@"%@",responseObject[@"data"][@"avatarUrl"]];
        }
        if (completion) completion(userInfo);
    } failure:^(NSError * _Nullable error) {
        if (completion) completion(nil);
    }];
}

#pragma mark -获取聊天客服信息
- (void)getChatKefuUrl:(void (^)(RCUserInfo *userInfo))completion {
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [params setObject:@1 forKey:@"code"];
    [[MBHTTPNetworking sharedInstance] WSAFNoOAuthUrlString:@"api/app/configs" method:@"POST" parameters:params success:^(id  _Nullable responseObject) {
        NSLog(@"");
        RCUserInfo *userInfo = [RCUserInfo new];
        NSInteger code = [responseObject[@"code"] integerValue];
        if (code == 0) {
            NSArray *dataArr = responseObject[@"data"];
            if (dataArr.count > 0) {
                NSDictionary *dict = dataArr[0];
                userInfo.portraitUri = [NSString stringWithFormat:@"%@",dict[@"iocn"]];
                userInfo.name = [NSString stringWithFormat:@"%@",dict[@"name"]];
                userInfo.userId = [NSString stringWithFormat:@"%@",dict[@"userCode"]];
            }
        }
        if (completion) completion(userInfo);
    } failure:^(NSError * _Nullable error) {
        if (completion) completion(nil);
    }];
}

#pragma mark -举报拉黑
- (void)reportBlockUserWithUserId:(NSString *)userId reason:(NSString *)reason type:(NSInteger)type success:(void(^)(BOOL success))resultBlock {
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [params setObject:userId forKey:@"userCodeFriend"];
    [params setObject:@(type) forKey:@"opType"];
    [params setObject:reason forKey:@"reason"];
    [[MBHTTPNetworking sharedInstance] WSAFNoOAuthUrlString:@"api/users/addAction" method:@"POST" parameters:params success:^(id  _Nullable responseObject) {
        NSInteger code = [responseObject[@"code"] integerValue];
        if (code == 0) {
            if (resultBlock) resultBlock(YES);
        } else {
            if (resultBlock) resultBlock(NO);
        }
    } failure:^(NSError * _Nullable error) {
        if (resultBlock) resultBlock(YES);
    }];
}

#pragma mark -获取用户在线状态
- (void)getUserOnlineWithUserCodes:(NSArray *)userCodes success:(void(^)(BOOL success))resultBlock {
    __WeakSelf
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [params setObject:userCodes forKey:@"userCodes"];
    [[MBHTTPNetworking sharedInstance] WSAFNoOAuthUrlString:@"api/chat/model/online" method:@"POST" parameters:params success:^(id  _Nullable responseObject) {
        NSInteger code = [responseObject[@"code"] integerValue];
        if (code == 0) {
            [weakSelf.onlineCodeArr removeAllObjects];
            NSArray *dataArr = responseObject[@"data"][@"onlineUserList"];
            if (dataArr.count > 0) {
                for (NSDictionary *dict in dataArr) {
                    [weakSelf.onlineCodeArr addObject:[NSString stringWithFormat:@"%@",dict[@"userCode"]]];
                }
            }
            if (resultBlock) resultBlock(YES);
        } else {
            if (resultBlock) resultBlock(NO);
        }
    } failure:^(NSError * _Nullable error) {
        if (resultBlock) resultBlock(YES);
    }];
}

#pragma mark -获取用户聊天权限次数 是否是match也是这里判断  type进入传1 离开传2
- (void)getUserMessageNumWithUserCode:(NSString *)userCode type:(NSInteger)type success:(void(^)(BOOL success))resultBlock {
    __WeakSelf
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [params setObject:userCode forKey:@"userCodeFriend"];
    [params setObject:@(type) forKey:@"type"];
    [[MBHTTPNetworking sharedInstance] WSAFNoOAuthUrlString:@"api/chat/openchat" method:@"POST" parameters:params success:^(id  _Nullable responseObject) {
        NSInteger code = [responseObject[@"code"] integerValue];
        if (code == 0) {
            weakSelf.isMatched = [responseObject[@"data"][@"matchFlag"] boolValue];
            weakSelf.messagesNumber = [responseObject[@"data"][@"messagesNumber"] integerValue];
            if (resultBlock) resultBlock(YES);
        } else {
            if (resultBlock) resultBlock(NO);
        }
    } failure:^(NSError * _Nullable error) {
        if (resultBlock) resultBlock(YES);
    }];
}

#pragma mark -发送私密照片
- (void)sendPrivatePhoteWithImageUrl:(NSString *)imageUrl type:(NSInteger)type videoLength:(NSInteger)videoLength success:(void(^)(BOOL success))resultBlock {
//    __WeakSelf
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [params setObject:imageUrl forKey:@"imageUrl"];
    [params setObject:@(type) forKey:@"type"];
    if (videoLength > 0) {
        [params setObject:@(videoLength) forKey:@"videoLength"];
    }
    [[MBHTTPNetworking sharedInstance] WSAFNoOAuthUrlString:@"api/users/addAlbums" method:@"POST" parameters:params success:^(id  _Nullable responseObject) {
        NSInteger code = [responseObject[@"code"] integerValue];
        if (code == 0) {
            if (resultBlock) resultBlock(YES);
        } else {
            if (resultBlock) resultBlock(NO);
        }
    } failure:^(NSError * _Nullable error) {
        if (resultBlock) resultBlock(YES);
    }];

}

#pragma mark -获取私密照片列表
- (void)getPrivatePhoteList:(void(^)(BOOL success))resultBlock {
    __WeakSelf
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [params setObject:@(3) forKey:@"type"];
    [[MBHTTPNetworking sharedInstance] WSAFNoOAuthUrlString:@"api/users/albums" method:@"POST" parameters:params success:^(id  _Nullable responseObject) {
        NSInteger code = [responseObject[@"code"] integerValue];
        if (code == 0) {
            [weakSelf.privateDataArr removeAllObjects];
            for (NSDictionary *modelDict in responseObject[@"data"][@"images"]) {
                NSPrivatePhotoModel *model = [NSPrivatePhotoModel mj_objectWithKeyValues:modelDict];
                if (model.imageUrl.length > 0) {
                    [weakSelf.privateDataArr addObject:model];
                }
            }
            if (resultBlock) resultBlock(YES);
        } else {
            if (resultBlock) resultBlock(NO);
        }
    } failure:^(NSError * _Nullable error) {
        if (resultBlock) resultBlock(NO);
    }];
}

#pragma mark -聊天列表获取fc信息
- (void)getChatListFCInfo:(void(^)(BOOL success))resultBlock {
    __WeakSelf
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [[MBHTTPNetworking sharedInstance] WSAFNoOAuthUrlString:@"api/auth/conversation/list" method:@"POST" parameters:params success:^(id  _Nullable responseObject) {
        NSInteger code = [responseObject[@"code"] integerValue];
        if (code == 0) {
            [weakSelf.fcCodeArr removeAllObjects];
            [weakSelf.onlineCodeArr removeAllObjects];
            for (NSDictionary *modelDict in responseObject[@"data"]) {
                if ([modelDict[@"flashchatFlag"] integerValue] == 1) {
                    [weakSelf.fcCodeArr addObject:[NSString stringWithFormat:@"%@",modelDict[@"userCodeFriend"]]];
                }
                
                if ([modelDict[@"onlineStatus"] integerValue] == 1) {
                    [weakSelf.onlineCodeArr addObject:[NSString stringWithFormat:@"%@",modelDict[@"userCodeFriend"]]];
                }
            }
            if (resultBlock) resultBlock(YES);
        } else {
            if (resultBlock) resultBlock(NO);
        }
    } failure:^(NSError * _Nullable error) {
        if (resultBlock) resultBlock(NO);
    }];
}



#pragma mark - Setter && Getter
- (NSMutableArray<MBChatNewFriendsModel *> *)newFriendsDataArr {
    if (!_newFriendsDataArr) {
        _newFriendsDataArr = [NSMutableArray array];
    }
    return _newFriendsDataArr;
}

- (NSMutableArray *)onlineCodeArr {
    if (!_onlineCodeArr) {
        _onlineCodeArr = [NSMutableArray array];
    }
    return _onlineCodeArr;
}

- (NSMutableArray<NSPrivatePhotoModel *> *)privateDataArr {
    if (!_privateDataArr) {
        _privateDataArr = [NSMutableArray array];
    }
    return _privateDataArr;
}

- (NSMutableArray *)fcCodeArr {
    if (!_fcCodeArr) {
        _fcCodeArr = [NSMutableArray array];
    }
    return _fcCodeArr;
}
@end
